//
//  SelectCollectionViewCell.swift
//  CTCollectionViewDemo
//
//  Created by GodCoder on 2019/8/9.
//  Copyright © 2019 CTCollectionView. All rights reserved.
//

import UIKit
import Photos

protocol photosCancelDelegate {
    func photosCancelAction(_ asset: PHAsset?)
}

class SelectCollectionViewCell: UICollectionViewCell {
    
    
//    public enum selectState {
//        case select
//        case DesSelect
//    }

    @IBOutlet weak var stateButton: UIButton!
    @IBOutlet weak var describeLab: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    
    var asset: PHAsset? = nil {
        didSet {
            setDetailContent()
        }
    }
    
    var selectState: Bool = false {
        willSet {}
        didSet {
            changeSelectState()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        imageView.layer.borderWidth = 0.5
        imageView.layer.borderColor = UIColor.gray.cgColor
    }
    
    fileprivate func changeSelectState() {
        stateButton.imageView?.image = UIImage.init(named: selectState ? "xuanzhong" : "xuanze")
    }
    
    fileprivate func setDetailContent() {
        
        self.describeLab.text = ""
        if asset!.mediaType == .image{
            let size = CGSize(width: asset!.pixelWidth, height: asset!.pixelHeight)
            PHCachingImageManager.default().requestImage(for: asset!, targetSize: size, contentMode: .aspectFill, options: .none) { (img, dic) in
                self.imageView.image = img
            }
        }
        else if asset!.mediaType == .video {
            let option = PHVideoRequestOptions()
            option.version = .current
            option.deliveryMode = .automatic
            option.isNetworkAccessAllowed = true
            
            PHCachingImageManager.default().requestAVAsset(forVideo: asset!, options: option) { (avsset, mix, dict) in
                if avsset != nil {
//                    guard let uslAsset: AVURLAsset = avsset as? AVURLAsset else {return}
                    
                    let generator = AVAssetImageGenerator.init(asset: avsset!)
                    generator.appliesPreferredTrackTransform = true
                    let time = CMTimeMakeWithSeconds(0.0,preferredTimescale: 600)
                    var actualTime:CMTime = CMTimeMake(value: 0,timescale: 0)
                    let imageRef:CGImage = try! generator.copyCGImage(at: time, actualTime: &actualTime)
                    
                    DispatchQueue.main.async {
                        self.imageView.image = UIImage.init(cgImage: imageRef)
                        let getSeconds = NSInteger(round(CMTimeGetSeconds(avsset!.duration)))
                        let seconds = getSeconds%60
                        let minutes = getSeconds%3600/60
                        let hours = getSeconds/3600
                        
                        if hours > 0 {
                            // MARK:--------------- TMDTMDTMDTMDTMDTMDTMDTMDTMDTMD ---------------
                            self.describeLab.text = "\(hours):\(minutes):\(seconds)"
                        } else {
                            if minutes < 10 && seconds < 10{
                                self.describeLab.text = "0\(minutes):0\(seconds)"
                            } else if minutes < 10 && seconds > 10{
                                self.describeLab.text = "0\(minutes):\(seconds)"
                            } else if minutes > 10 && seconds < 10{
                                self.describeLab.text = "\(minutes):0\(seconds)"
                            } else {
                                self.describeLab.text = "\(minutes):\(seconds)"
                            }
                        }
                    }
                }
            }
        }
    }
}
